﻿using PerpheralDevice.IRepository;
using System.Runtime.CompilerServices;

namespace PerpheralDevice;

/// <summary>
/// 输入、输出外部设备
/// </summary>
public abstract class PerpDevIO: PerpDev,IDisposable {
    protected bool mfIsPolling = true;
    protected Task mfPollingTask = null;
    ~PerpDevIO() {
        this.Dispose(isFinalizer: true);
    }
    public void Dispose() {
        this.Dispose(isFinalizer: false);
        GC.SuppressFinalize(this);
    }
    /// <summary>
    /// 释放非托管资源;等待独立线程退出
    /// </summary>
    /// <param name="isFinalizer">是否为终结器(析构函数)回调</param>
    [MethodImpl(MethodImplOptions.Synchronized)]
    private void Dispose(bool isFinalizer) {
        this.mfIsPolling = false;
        if (isFinalizer) {
            if (!this.mfIsPolling) {
                this.mfPollingTask?.Wait();
            }
        }
    }
}
